package com.hlk.article.controller;

import com.hlk.pojo.Article;
import com.hlk.pojo.Spouse;
import com.hlk.pojo.Stu;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-12-15 15:43
 */
@Controller
@RequestMapping("freemarker")
public class FreemarkerController {

    @Value("${freemarker.target.path}")
    private String targetPath;

    @ResponseBody
    @GetMapping("createHtml")
    public String createHtml(Model model) throws Exception {
        Configuration configuration = new Configuration(Configuration.getVersion());
        // 声明freemarker模板所需要加载的目录位置
        String classpath = this.getClass().getResource("/").getPath();
        configuration.setDirectoryForTemplateLoading(new File(classpath + "templates"));
        // 获取现有ftl模板
        Template template = configuration.getTemplate("hello.ftl", "utf-8");
        // 获得动态数据
        String str = "凯哥新闻";
        model.addAttribute("title", str);
        makeModel(model);

        // 融合动态数据和ftl生成html
        File file = new File(targetPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        FileWriter out = new FileWriter(targetPath + File.separator + "1.html");
        template.process(model, out);
        out.close();

        return "ok";
    }


    @GetMapping("hello")
    public String hello(Model model) {
        String str = "凯哥新闻，你好";
        model.addAttribute("title", str);
        makeModel(model);

        return "hello";
    }

    private void makeModel(Model model) {
        Stu stu = new Stu();
        stu.setUid("1001");
        stu.setUsername("hlk");
        stu.setAmount(88.88f);
        stu.setAge(19);
        stu.setHaveChild(true);
        stu.setBirthday(new Date());

        Spouse spouse = new Spouse();
        spouse.setAge(18);
        spouse.setUsername("Lili");

        stu.setSpouse(spouse);

        stu.setArticleList(makeList());

        stu.setParents(makeMap());

        model.addAttribute("stu", stu);
//        return model;
    }

    private List<Article> makeList() {
        Article article = new Article();
        article.setId("1");
        article.setTitle("三体");
        Article article2 = new Article();
        article2.setId("2");
        article2.setTitle("三体2");
        Article article3 = new Article();
        article3.setId("3");
        article3.setTitle("三体3");
        List<Article> list = new ArrayList<>();
        list.add(article);
        list.add(article2);
        list.add(article3);
        return list;
    }

    private Map<String, String> makeMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("father", "Lilei");
        map.put("mother", "hanmeimei");
        return map;
    }

}
